package com.it.controller;

import com.it.mapper.DeptMapper;
import com.it.pro.Dept;
import com.it.pro.Result;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptMapper deptMapper;
    @GetMapping("/depts")
    public Result findAll(){
        //查询所有部门信息
        List<Dept> deptList = deptMapper.findAll();
        //返回
        return Result.success(deptList);
    }
    @DeleteMapping("/depts/{id}")
    public Result deleteById(@PathVariable Integer id){
        //删除部门信息
        deptMapper.deleteById(id);
        //返回
        return Result.success();
    }
    @PostMapping("/depts")
    public Result save(@RequestBody Dept dept){
        //补全信息
        //dept.setCreateTime(LocalDateTime.now());
        //dept.setUpdateTime(LocalDateTime.now());
        //调用mapper完成新增
        //保存部门信息
        deptMapper.save(dept);
        //返回
        return Result.success();
    }
    @GetMapping("/depts/{id}")
    public Result findById(@PathVariable Integer id){
        //根据id查询部门信息
        Dept dept = deptMapper.findById(id);
        //返回
        return Result.success(dept);
    }
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        //补全信息
        //dept.setUpdateTime(LocalDateTime.now());
        //调用mapper完成修改
        //修改部门信息
        deptMapper.update(dept);
        //返回
        return Result.success();
    }
}
